package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.MidtermDateSet;
import com.xmy.cultivate.service.IMidtermDateSetService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 期中时间设置 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-11-21
 */
@RestController
@RequestMapping("/api/midtermDateSet")
@Scope("prototype")
public class MidtermDateSetController  extends BaseController{
    @Autowired
    IMidtermDateSetService iMidtermDateSetService;

    @GetMapping("")
    public Result findAllPage(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                              @RequestParam(value = "yearPart", required = false) String yearPart,  //年份
                              @RequestParam(value = "quarterNum", required = false) String quarterNum, //学期
                              @RequestParam(value = "gradeSection", required = false) String gradeSection //学段
                               ){
        Page<MidtermDateSet> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper<MidtermDateSet> midtermDateSetQueryWrapper = new QueryWrapper<>();

        midtermDateSetQueryWrapper.eq(StringUtils.isNotBlank(yearPart),"year_part",yearPart);
        midtermDateSetQueryWrapper.eq(StringUtils.isNotBlank(quarterNum),"quarter_num",quarterNum);
        midtermDateSetQueryWrapper.eq(StringUtils.isNotBlank(gradeSection),"grade_section",gradeSection);
        midtermDateSetQueryWrapper.eq("deleted",0);
        midtermDateSetQueryWrapper.orderByDesc("created_at");
        iMidtermDateSetService.page(iPage,midtermDateSetQueryWrapper);

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }
    /**
     * 期中时间设置添加
     */
    @PostMapping("/saveMidtermDateSet")
    public Result save(@RequestBody MidtermDateSet midtermDateSet) {
        if (midtermDateSet.getYearPart()== null ) {
         return new Result(Code.SAVE_ERROR, "", "年份不能为空");
        }
        if (midtermDateSet.getQuarterNum()== null ){
            return new Result(Code.SAVE_ERROR, "", "学期不能为空");
        }
        if (midtermDateSet.getGradeSection()== null){
            return new Result(Code.SAVE_ERROR, "", "学段不能为空");
        }
        QueryWrapper<MidtermDateSet> midtermDateSetQueryWrapper = new QueryWrapper<>();
        midtermDateSetQueryWrapper.eq("year_part",midtermDateSet.getYearPart());
        midtermDateSetQueryWrapper.eq("quarter_num",midtermDateSet.getQuarterNum());
        midtermDateSetQueryWrapper.eq("grade_section",midtermDateSet.getGradeSection());
        midtermDateSetQueryWrapper.eq("deleted",0);
        long count = iMidtermDateSetService.count(midtermDateSetQueryWrapper);
        if (count > 0) {
             return new Result(Code.SAVE_ERROR, "", "该学期该学段期中时间已设置过，请勿重复设置");
        }
        boolean save = iMidtermDateSetService.save(midtermDateSet);
        if (save) {
             return new Result(Code.SAVE_OK, "", "添加成功");
        }
         return new Result(Code.SAVE_ERROR,"","添加失败");
    }

    @PutMapping("/updateMidtermDateSet")
    @ApiOperation("修改")
    public Result update(@RequestBody MidtermDateSet midtermDateSet) {
        MidtermDateSet midtermDateSet1 = iMidtermDateSetService.getById(midtermDateSet.getId());
        if (midtermDateSet1 == null) {
            return new Result(Code.UPDATE_ERROR, null, "该条记录不存在");
        }
        QueryWrapper<MidtermDateSet> midtermDateSetQueryWrapper = new QueryWrapper<>();
        midtermDateSetQueryWrapper.eq("year_part",midtermDateSet.getYearPart());
        midtermDateSetQueryWrapper.eq("quarter_num",midtermDateSet.getQuarterNum());
        midtermDateSetQueryWrapper.eq("grade_section",midtermDateSet.getGradeSection());
        midtermDateSetQueryWrapper.eq("deleted",0);
        long count = iMidtermDateSetService.count(midtermDateSetQueryWrapper);
        if (count > 0) {
             return new Result(Code.SAVE_ERROR, "", "该学期该学段期中时间已设置过，请勿重复设置");
        }
        boolean update = iMidtermDateSetService.updateById(midtermDateSet);
        if (update ) {
            return new Result(Code.UPDATE_OK, "", "编辑成功");
        }
        return new Result(Code.UPDATE_ERROR,"","编辑失败");
    }


}
